import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Table, Row, Col} from 'antd';
import {TABLE, DATE_FORMAT} from '../constants';
import {formatAmount, formatDateTime} from '../utils';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';

export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
    };
    componentDidMount() {
        this.props.init();
    }
    render() {
        const detailData = this.props.detail || {};
        const {formatMessage} = this.props.intl;
        const columns = [{
            title: formatMessage({
                id: 'detailPanel.column.partInvoiceCode',
                defaultMessage: '发票号'
            }),
            dataIndex: 'partInvoiceCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.partSaleOrderCode',
                defaultMessage: '订单号'
            }),
            dataIndex: 'partSaleOrderCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.partOrderTypeName',
                defaultMessage: '订单类型'
            }),
            dataIndex: 'partOrderTypeName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.submitTime',
                defaultMessage: '提报日期'
            }),
            dataIndex: 'submitTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.creditAuditedTime',
                defaultMessage: '信用审核日期'
            }),
            dataIndex: 'creditAuditedTime',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.partCode',
                defaultMessage: '备件编号'
            }),
            dataIndex: 'partCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.partName',
                defaultMessage: '备件名称'
            }),
            dataIndex: 'partName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.englishName',
                defaultMessage: '备件英文名称'
            }),
            dataIndex: 'englishName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.price',
                defaultMessage: '单价'
            }),
            dataIndex: 'price',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.minPackSize',
                defaultMessage: '最小包装数'
            }),
            dataIndex: 'minPackSize',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.quantity',
                defaultMessage: '数量'
            }),
            dataIndex: 'quantity',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.totalAmount',
                defaultMessage: '金额'
            }),
            dataIndex: 'totalAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.oldPartCode',
                defaultMessage: '被替换件'
            }),
            dataIndex: 'oldPartCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.palletCode',
                defaultMessage: '托盘号'
            }),
            dataIndex: 'palletCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.perNetWeight',
                defaultMessage: '净重(KG)'
            }),
            dataIndex: 'perNetWeight',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.netWeight',
                defaultMessage: '总净重(KG)'
            }),
            dataIndex: 'netWeight',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.categoryCode',
                defaultMessage: '维护车型'
            }),
            dataIndex: 'categoryCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.isEOL',
                defaultMessage: '是否停产'
            }),
            dataIndex: 'isEOL',
            render: text => text === true ? '是' : '否',
        }];
        return (
            <div className="detail">
                <Card loading={this.props.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{
                        formatMessage({
                            id: 'detailPanel.card.partInvoiceTrack',
                            defaultMessage: '备件出运跟踪详情'
                        })
                    }</Col></Row>}>
                        <Table
                            className="white-space-nowrap"
                            columns={columns}
                            rowKey="id"
                            pagination={false}
                            dataSource={detailData}
                            {...TABLE} />
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPanel.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
};

import {connect} from 'react-redux';
import {getDetail} from './actions';
import {injectIntl} from './intl';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);
const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    permissions: getPermissions(state),
    detail: getDetailData(state),
    isDealerUser: state.getIn(['page', 'domainData', 'init', 'isDealerUser']),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
